1

更新,更清楚:

我当前包含值“1201026404”的字段(每次都会更改):

<input id="ticket_fields_20323656" name="ticket[fields][20323656]" size="30" style="width:125px;" type="text" value="1201026404" tabindex="11">

当页面加载时,我希望复制的值“1201026404”(每次都会改变)去的 LI:

<ul class="multi_value_field" style="width: 99.5%;">
<li class="choice" choice_id="1201026404">1201026404<a class="close">×</a><input type="hidden" name="ticket[set_tags][]" value="1201026404" style="display: none;"></li>
</ul>

我已经制作但需要帮助的 Javascript:

<script type="text/javascript">
copy = function()
{
    var n1 = document.getElementById("ticket_fields_20323656");
    var n2 = ‘what goes here??’
    n2.value = n1.value;
}
</script>
4

2 回答 2

0

您将不得不在 li 元素上使用 ID。

 <li id="choice_20323656">

然后你可以像这样复制它

<script type="text/javascript">
copy = function()
{
    var n1 = document.getElementById("ticket_fields_20323656");
    var n2 = document.getElementById("choice_20323656");
    n2.innerHTML = n1.value;
}
</script>

编辑:

如果您可以使用 jQuery 1.6 或更高版本,这将起作用:

$("#ticket_fields_20323656").keyup(function(e) {
    $(".choice")
        .attr("choice_id",e.currentTarget.value)
        .html(e.currentTarget.value
              + "<a class=\"close\">×</a><input" 
              + " type=\"hidden\" name=\"ticket" 
              + "[set_tags][]\" value=\"" 
              + e.currentTarget.value 
              + "\" style=\"display: none;\">");
});​

这是一个演示: http: //jsfiddle.net/JKirchartz/V2L25/,但是你应该知道,如果你有多个类的东西,choice它们的值会以同样的方式改变:http: //jsfiddle.net/ JKirchartz/V2L25/4/

于 2012-11-13T03:48:34.370 回答
0

这在 Firefox 中有效吗:

<script type="text/javascript">
copy = function()
{
    var n1 = document.getElementById("ticket_fields_20323656");
    var n2 = document.querySelectorAll("ul.multi_value_field li:first");
    n2.innerHTML = n1.value;
}
</script>

this uses querySelectorAll which doesnt exist in IE6,7,8 - otherwise you need to use the id of the element which im not sure you have

于 2012-11-13T03:49:38.700 回答