【问题标题】:Modification: Change from Input to Span and jquery /javascript is not finding and setting element value修改:从 Input 更改为 Span 和 jquery /javascript 未找到和设置元素值
【发布时间】:2015-09-22 17:12:46
【问题描述】:

我确实有可以工作的代码,但我需要修改它

http://jsfiddle.net/bthorn/xbs5c41b

然后我被告知我需要从输入更改为标签(跨度)

现在即使我从

更改,此代码也不起作用

http://jsfiddle.net/bthorn/xbs5c41b/1/

.find("input")

.find("span")

注意我注释掉的代码行

 function setDateTimeOn(elm) {
        var formattedDate = GetCurrentDateTime(); //get formatted date
        $(elm) //clicked button
            .parent("td") // container td
            .next() // next td
            //.find("input") // find child input
            .find("span")
            .val(formattedDate); //set date
    }

HTML

<table>
<tr>
    <td style="width:5px;">
        <input type="button" id="GridView1__ctl2_AddButton0" name="GridView1:_ctl2:AddButton0" value="On" class="btn-blue" onclick="setDateTimeOn(this)">
    </td>
    <td style="width:150px;">
       <!-- <input id="GridView1__ctl2_txtStormTimeOn" type="text" name="GridView1:_ctl2:txtStormTimeOn" value="">-->
        <span id="GridView1__ctl2_lblStormTimeOn"></span> 
    </td>
</tr>

“.find() 不能在 span 上工作吗?

【问题讨论】:

标签: jquery


【解决方案1】:

find 方法有效。

您的代码的问题是 val 一个,但它没有,因为它试图在元素上找到一个 value 属性,虽然 HTMLInputElement 有一个,但 HTMLSpanElement 没有.

您必须将其更改为使用text 方法。

/* (...) */
.find("span")
.text(formattedDate); // set date

这里是your fiddle updated

【讨论】:

  • 我做到了,总是要等 10 分钟才能接受 BTW thx !
【解决方案2】:

span 元素没有与您的输入嵌套,因此您必须使用 parent() 来达到它的范围。 另外,您不能将 val 与 span 一起使用,例如 div 您必须使用 text() 或 html() 来代替:

function setDateTimeOn(elm) {
    var formattedDate = GetCurrentDateTime(); //get formatted date
    $(elm) //clicked button
        .parent("td") // container td
        .next() // next td
        //.find("input") // find child input
         .parent().find("span")
        .html(formattedDate); //set date
}

【讨论】:

  • 我不熟悉 .parent().find("span") .html(formattedDate); //设置日期它可能有效,但我已经测试了其他代码,虽然对我来说效果很好,但是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
相关资源
最近更新 更多