简体   繁体   English

如何将下拉列表中选定项的值替换为jQuery中文本框的更改事件的某个值?

[英]how to replace value of drop down list selected item to some value on change event of text box in jquery?

I have a dropdown list and a textbox. 我有一个下拉列表和一个文本框。 I am trying to replace the dropdown list selected value to some value by changing the text in textbox. 我试图通过更改文本框中的文本将下拉列表中的选定值替换为某些值。

Here is my code: 这是我的代码:

<select class="form-control Series-1 valid" name="CRMFields" id="CRMFields">
    <option>None</option>
    <optgroup label="Lead Information"></optgroup>
    <option value="First Name^Lead ID^">Lead ID</option>
    <option value="First Name^First Name^" selected="">First Name</option>
    <option value="First Name^Last Name^">Last Name</option>
</select>
<input class="defaultValue" data-target-class="Series-1" id="DefaultValue" name="DefaultValue" type="text" value="">

JQuery: JQuery的:

<script type="text/javascript">
    $(document).ready(function () {
        $(".defaultValue").change(function () {
            var thisval = $(this).val();
            var targetclass = $(this).attr("data-target-class");
            var CRMFiledValue = $("." + targetclass).val();
            var arr = CRMFiledValue.split('^');
            var left = arr[0];
            var middle = arr[1];
            var last = thisval;
            var replaceText = left + "^" + middle + "^" + last;
            alert(replaceText);
            alert($("." + targetclass).prop("value", replaceText));
            alert($("." + targetclass).val());               
        });
    });
</script>

But above code showing me [object Object] and null on the alert box instead of concatenated value. 但是上面的代码在警告框中显示了[object Object]和null而不是串联值。

I want to replace the selected item of value to some value on Change event of text box. 我想在文本框的更改事件中将选定的值项替换为某个值。

JSFiddle 的jsfiddle

I appreciate your help in advance. 非常感谢您的帮助。

Part of the problem is that you are changing the value of every single option whenever someone changes the value of the input box. 问题的一部分是,每当有人更改输入框的值时,您都在更改每个选项的值。

so I added + option:selected to the selector of the var CRMFiledValue . 所以我在var CRMFiledValue的选择器中添加了+ option:selected I assume this is what you were going for because you probably didn't want every field to have FirstName^FirstName^thisval as it's value. 我认为这就是您FirstName^FirstName^thisval原因,因为您可能不希望每个字段都具有FirstName^FirstName^thisval作为其值。 The below code was tested and worked according to how your HTML/other javascript was set up 以下代码已根据您的HTML /其他javascript的设置进行了测试和工作

  $(document).ready(function () { $(".defaultValue").change(function () { var thisval = $(this).val(); var targetclass = $(this).attr("data-target-class"); var CRMFiledValue = $("." + targetclass + ' option:selected').val(); var arr = CRMFiledValue.split('^'); var left = arr[0]; var middle = arr[1]; var last = thisval; var replaceText = left + "^" + middle + "^" + last; $("." + targetclass + ' option:selected').prop("value", replaceText); }); }); 

You should change: 您应该更改:

$("." + targetclass).prop("value", replaceText);

To: 至:

$("." + targetclass + ' option:selected').val(replaceText);

And always bear in mind that this operation does not return a scalar value and will therefore return [Object Object] to alert( ... ) . 并始终牢记此操作不会返回标量值,因此会将[Object Object]返回给alert( ... ) A better debug function would be console.log( .... ) : 更好的调试功能是console.log( .... )

DEMO DEMO

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

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