简体   繁体   English

在进行选择更改时更改动态div内的文本jQuery

[英]Change text inside dynamic div when on select change is made jquery

I have a title for my drop down that is dynamically given and I can currently change it when the page loads. 我有一个下拉菜单标题,它是动态给出的,当前可以在页面加载时更改它。 But once I select an item from the drop down it changes back. 但是,一旦我从下拉菜单中选择了一项,它就会变回来。 I am looking for a simple jquery js solution that can help keep the name when an item is selected. 我正在寻找一个简单的jquery js解决方案,当选择一个项目时可以帮助保留名称。

I need to change the text in: #shippingStateSpan from Destination State -> Destination Province and leave that way even after something is selected. 我需要在“目的地国家->目的地省份”中更改#shippingStateSpan中的文本,并且即使选择了某些内容,也要保持这种方式。

Here is my html: 这是我的html:

<div class="shippingStateDiv">
    <span id="shippingStateSpan">Destination State<br />
    </span>
    <select name="shippingState" id="shippingState" class="shippingDropDown"
         onchange="ApplyTaxRate(this.value,4040828,1,1,0);">
             <option value="-1" selected="selected">Please Select</option>
             <option value="129654">AB</option>
             <option value="129653">BC</option>
             <option value="129652">MB</option>
             <option value="129647">NB</option>
    </select>
</div>

Here is my js I use to make the change on initially (But it changes back after I select something) I assume a jquery .on function may be possible but I an not sure how to do it. 这是我最初用来进行更改的js(但是在选择某些内容后它会变回)我假设可以使用jquery .on函数,但是我不确定该怎么做。 Thanks for the help. 谢谢您的帮助。

<script type="text/javascript">
     $("#shippingStateSpan").text("Destination Province");
</script>

Try this: 尝试这个:

$(document).ready(function(){

  var $select = $('#shippingState'),
      $span = $('#shippingStateSpan');

  $select.on('change', function(){

    $span.html('Destination Province');

  });

});

Try this 尝试这个

​$(function() {

    $('#shippingState').on('change', function() {

        $('#shippingStateSpan').text('Destination Province')
    });

});​

Check out the working example here http://jsfiddle.net/sushanth009/zp7bA/ 在这里查看工作示例http://jsfiddle.net/sushanth009/zp7bA/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM