如何将响应值分配给变量? [英] how to assign the response value to variable?

查看:106
本文介绍了如何将响应值分配给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   script    类型  =  text / javascript  >  
function getCombo1(sel)
{
var value = sel.options [sel.selectedIndex] .value;
$( #test)。load( test.aspx?test = + value);

}


< / script >

< / head >
< 正文 >


< 选择 id = combo1 onchange = getCombo1 (this) >
< 选项 value = ka > Karnataka < / option >
< 选项 value = tn > Tamilnadu < / option >
< span class =code-keyword>< 选项 value = ap > Andrapradesh < / option >
< 选项 < span class =code-attribute> value = mh > Maharashtra < / option >
< / select >

< pre id = test >





< / body>

它将在p tag $中显示输出b $ b但是我希望它显示在这个
< select id =Select1>
< option>< / option>
< / select>

解决方案

< span class =code-string> #test)。load( test.aspx?test = +值);

}


< / script >

< / head >
< 正文 >


< 选择 id = combo1 onchange = getCombo1 (this) >
< 选项 value = ka > Karnataka < / option >
< 选项 value = tn > Tamilnadu < / option >
< span class =code-keyword>< 选项 value = ap > Andrapradesh < / option >
< 选项 < span class =code-attribute> value = mh > Maharashtra < / option >
< / select >

< pre id = test >





< / body>

它将在p tag $中显示输出b $ b但是我希望它显示在这个
< select id =Select1>
< option>< / option>
< / select>


这不完全是组合框;这是HTML 选择元素。通过显示,您可能意味着选择项目(选项元素)取决于您的某些价值



Of当然,你不能在这里使用jQuery .load 。请看它的作用:

https://api.jquery.com/load [ ^ ]。



它在元素中放置了一些HTML。这不是你想要的。您想从一个选项元素中删除选定的属性,并将此属性添加到其他一些(或可能相同)元素,或者没有,如果没有匹配。



这里解释了这些操作的实现;请参阅问题的最佳答案: http://stackoverflow.com/questions/314636/how-do-you-select-a-particular-option-in-a-select-element-in-jquery [ ^ ]。



而不是 .load ,您需要从服务中提取值。使用通用jQuery Ajax: http://api.jquery.com/jquery.ajax [ ^ ]。



那是所有。正如你所看到的,这比 .load 要复杂得多,但没什么太难的。



-SA

<script type="text/javascript">
    function getCombo1(sel)
  {
        var value = sel.options[sel.selectedIndex].value;
        $("#test").load("test.aspx?test=" + value);
      
    }
    

</script>

</head>
<body>
    
    
<select id="combo1" onchange="getCombo1(this)">
  <option value="ka">Karnataka</option>
  <option value="tn">Tamilnadu</option>
   <option value="ap">Andrapradesh</option>
  <option value="mh">Maharashtra</option>
</select>

  <pre id="test">



</body>

it will display the output in p tag
but i want it to b displayed in this 
<select id="Select1">
        <option></option>
    </select>

解决方案

("#test").load("test.aspx?test=" + value); } </script> </head> <body> <select id="combo1" onchange="getCombo1(this)"> <option value="ka">Karnataka</option> <option value="tn">Tamilnadu</option> <option value="ap">Andrapradesh</option> <option value="mh">Maharashtra</option> </select> <pre id="test">



</body>

it will display the output in p tag
but i want it to b displayed in this 
<select id="Select1">
        <option></option>
    </select>


This is not exactly "combo box"; this is HTML select element. By "display", you may mean selecting the item (option element) depending on some value you

Of course, you cannot use jQuery .load here. Please see what it does:
https://api.jquery.com/load[^].

It places some HTML in the element. This is not what you want. You want to remove selected attribute from one option element and either add this attribute to some other (or maybe the same) element, or none of them, in case there is no match.

The implementations of these operations are explained here; please see top answer to the question: http://stackoverflow.com/questions/314636/how-do-you-select-a-particular-option-in-a-select-element-in-jquery[^].

Instead of .load, you need to just extract the value from service. Use general-purpose jQuery Ajax: http://api.jquery.com/jquery.ajax[^].

That's all. As you can see, this is considerably more tricky than .load, but nothing too hard.

—SA


这篇关于如何将响应值分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆