[英]Selecting part of text from list item for the text box
I have following code for the textbox: 我有以下文本框代码:
<label>Enter the doctor id</label>
<input type="text" autocomplete="on" name="dr_id" id="user_id" class="user_name" >
I want to add the string after '-' to the text box.For eg: for Dr. Prashant Salunke-dr.salunke .I must only have dr.salunke in the text box as it is after '-'. 我想在字符串“-”后添加字符串。例如:对于Dr. Prashant Salunke-dr.salunke,我只能在文本框中输入dr.salunke,因为它在“-”之后。 I have written some code given below:
我写了下面的一些代码:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
$('#list li').click(function()
{
var arr=$(this).text().split('-');
$('#user_id').val($(arr[1]).text());
});
});
</script>
<a href="#" class="show_hide">List of doctors</a><br />
<div class="slidingDiv">
<ul id="list">
<li style="cursor:hand;">Dr.Prashant Salunke - dr.salunke.</li>
<li style="cursor:hand;">Dr.Kalam - dr.apj</li>
<li style="cursor:hand;">Dr.Manmohan Singh - dr.economics</li>
</ul>
</div>
</div>
But it is not working. 但这是行不通的。 Please suggest some solution.
请提出一些解决方案。
The arr[1]
value does not need to be converted to a jQuery object. arr[1]
值不需要转换为jQuery对象。 You can use it as it is to set the val()
of the input
, although using $.trim
to remove leading/trailing spaces may help: 您可以按原样使用它来设置
input
的val()
,尽管使用$.trim
删除前导/尾随空格可能会有所帮助:
$('#list li').click(function() {
var arr = $(this).text().split('-');
$('#user_id').val($.trim(arr[1]));
});
You can do this: 你可以这样做:
$('#list li').click(function () {
var arr = $(this).text().split('-');
$('#user_id').val(arr[1]);
// arr returns ["Dr.Prashant Salunke ", " dr.salunke."]
// arr[1] returns dr.salunke.
});
As, arr[1]
has already the text value, there is no need to do $(arr[1]).text()
for getting the text value from it. 由于
arr[1]
已经具有文本值,因此无需执行$(arr[1]).text()
从中获取文本值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.