佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 880|回复: 13

如何在php/html随时 Add input type (Text)

[复制链接]
发表于 22-9-2008 12:09 PM | 显示全部楼层 |阅读模式

图上...
我想做一个 (+) Add
比如说, 我要加多几个email, 当我一按那个 (+) Add就会多一个textfield属于email的在下面出来..
那我就不用一次过放那么多input type (text) 出来...
需要事就按 (+) Add
不知道要如何才能做到呢???
希望可以得到好心人指导....

[ 本帖最后由 wcpon 于 22-9-2008 12:18 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 22-9-2008 12:56 PM | 显示全部楼层
如果想做DHTML, 就要用Java Script..
回复

使用道具 举报

 楼主| 发表于 22-9-2008 01:58 PM | 显示全部楼层
原帖由 jasonmun 于 22-9-2008 12:56 PM 发表
如果想做DHTML, 就要用Java Script..


你有什么example可以让我参考吗?
回复

使用道具 举报

发表于 22-9-2008 02:15 PM | 显示全部楼层
原帖由 wcpon 于 22-9-2008 01:58 PM 发表


你有什么example可以让我参考吗?

如果你会jQuery的话,可以用 jQuery Flydom.
要不然,你try去http://bytes.com/forum/thread692575.html 看看吧~
如果还不明白你可以去http://www.w3schools.com/htmldom/dom_examples.asp 参考html DOM 。。。
回复

使用道具 举报

 楼主| 发表于 22-9-2008 03:24 PM | 显示全部楼层
<html>
<head>
<title></title>
<SCRIPT TYPE="text/javascript">
var arrInput = new Array(0);
  var arrInputValue = new Array(0);
function addInput() {
  arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();
}
function display() {
  document.getElementById('parah').innerHTML="";
  for (intI=0;intI<arrInput.length;intI++) {
    document.getElementById('parah').innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
  }
}
function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
}  
function createInput(id,value) {
  return "<input type='text' id='test "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'><br>";
}
function deleteInput() {
  if (arrInput.length > 0) {
     arrInput.pop();
     arrInputValue.pop();
  }
  display();
}
</script>
</head>
<body>
<form name="testing" ACTION="test1.php" METHOD="post">
<p id="parah">Click below to dynamically create/remove input boxes in this field</p>
<a href="javascript:addInput()">Add more input field(s)</a><br>
<a href="javascript:deleteInput()">Remove field(s)</a>

<input type="submit" name="submit" value="Add">
</form>
</body>
</html>


我找到这个code..
可以add..
但是..
我不知道如何拿input text里面的value...
有人可以教教我吗???
如果我add 5 个input text, 我只能拿到其中一个..... :(

[ 本帖最后由 wcpon 于 22-9-2008 03:32 PM 编辑 ]
回复

使用道具 举报

发表于 22-9-2008 04:58 PM | 显示全部楼层
原帖由 wcpon 于 22-9-2008 03:24 PM 发表


我找到这个code..
可以add..
但是..
我不知道如何拿input text里面的value...
有人可以教教我吗???
如果我add 5 个input text, 我只能拿到其中一个.....


有看到 id='test "+ id吗? id是looping 里的increment variable...
我们须要从 id上着手。。。
比如,我add了五个。。。如果,我要拿第一个的value。。。
code 是如此:
oElement=document.getElementById('test0');
sValue = oElement.value;
//sValue contains the value.
如果你要拿全部,你需要知道现在有多少个 textbox, 然后用looping 那每一个的value.

不过,如果用jQuery的话,很简单。
code如下:
oElements = $("input[@id*="test"]);
oElements.each(
    function(element){
        sValue += " " + $(element).val();
   };
);
回复

使用道具 举报

Follow Us
 楼主| 发表于 22-9-2008 06:47 PM | 显示全部楼层
function addInput() {
if (arrInput.length > 2) {
alert('Warning! Maximum 3 Emails can be add per person.')
arrInput.length = 2;
}
  arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();

}


function createInput(id,value) {
  return "<input type='text' name='status[]' id='test "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'><br>";
}


我这样做..
我要限制它只能进3个input text....
然后我用status[ ], 带那个value过去..
然后我用foreach, call 回那些value出来....

这样的做法.... 我试过是行得通....
想问你意见... 你觉得如何??
回复

使用道具 举报

发表于 22-9-2008 07:52 PM | 显示全部楼层
原帖由 wcpon 于 22-9-2008 06:47 PM 发表


我这样做..
我要限制它只能进3个input text....
然后我用status[ ], 带那个value过去..
然后我用foreach, call 回那些value出来....

这样的做法.... 我试过是行得通....
想问你意见... 你觉得如何??

还不错...加油~
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 23-9-2008 09:36 AM | 显示全部楼层
原帖由 0buglogger 于 22-9-2008 07:52 PM 发表

还不错...加油~


那好的...
谢谢你哦..
回复

使用道具 举报

 楼主| 发表于 23-9-2008 02:57 PM | 显示全部楼层
想请问...
如果我要它每次加入新的input text时...
都是create一行row出来....
因为现在这样加.... 它只是在同一个row..
我要每个input text属于自己一个row...
可以做到吗??
我试了很久都不可以....
回复

使用道具 举报

发表于 23-9-2008 10:31 PM | 显示全部楼层
原帖由 wcpon 于 23-9-2008 02:57 PM 发表
想请问...
如果我要它每次加入新的input text时...
都是create一行row出来....
因为现在这样加.... 它只是在同一个row..
我要每个input text属于自己一个row...
可以做到吗??
我试了很久都不可以....
你是在用 jquery 吧。
是用 append 吗?在你加入之前放一个 <br /> 咯
要不然就用 div fix size, 什么input 都加在div 里面
回复

使用道具 举报

 楼主| 发表于 24-9-2008 09:33 AM | 显示全部楼层
原帖由 vampcheah 于 23-9-2008 10:31 PM 发表
你是在用 jquery 吧。
是用 append 吗?在你加入之前放一个  咯
要不然就用 div fix size, 什么input 都加在div 里面


我是用着上面那个自己的code...
我加input text是可以...
但是, 当我加时, 弄到那些table 乱..
因为它不在table 里面的....
我想每次加input text时... 连同row 一起加....
不知道你明白吗??

              input text 1
Email:   input text 2
              input text 3

问题是这样.... 我email那个column会随着input text增加而跑位.....


我要的是这样...

Email:  input text 1
             input text 2
             input text 3
当每加一次... 就在email下面add row和add input text...
这样的话, 我的email column就不会随着input text的增加而影响到了.....
回复

使用道具 举报

发表于 24-9-2008 04:37 PM | 显示全部楼层
这个问题其实很多人都会犯。
主要是因为你没有define 你的 css。
在你的table 先把 所有的 td 都设定成 valign="top"
当然你也可以在 css 里面预先设定(省功夫)
回复

使用道具 举报

 楼主| 发表于 25-9-2008 10:54 AM | 显示全部楼层
原帖由 vampcheah 于 24-9-2008 04:37 PM 发表
这个问题其实很多人都会犯。
主要是因为你没有define 你的 css。
在你的table 先把 所有的 td 都设定成 valign="top"
当然你也可以在 css 里面预先设定(省功夫)


明白了..
谢谢...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-12-2025 03:27 AM , Processed in 0.372883 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表