最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Losing text after a space setting the value in a new field - Stack Overflow

programmeradmin9浏览0评论

I am having an odd issue when using Javascript to create a new field. I have button that, when clicked, will create two text fields. I give those text fields a string as a value. However, it is only working correctly if I give it a string with no spaces. If my string contains a space then it makes the value equal to the text before the space occurs.

function nameField() {
  $('#nameArea').html("<br/>First Name:<input type='text' id='fnField' value=" + $('#firstName').text() + ">Last Name:<input type='text' id='lnField' value=" + $('#lastName').text() + "></input><button id='bName' onclick='updateName(fnField.value, lnField.value)'>Save Changes</button>");
  $('#firstName').html("");
  $('#lastName').html(""); 
}

So for example, if the firstName value is David E, it is currently only putting David in the field as the value. However, I have used alert and can confirm that $('#firstName').text(); does contain the full David E. How can I make sure that the text after the space doesn't get slashed out?

I am having an odd issue when using Javascript to create a new field. I have button that, when clicked, will create two text fields. I give those text fields a string as a value. However, it is only working correctly if I give it a string with no spaces. If my string contains a space then it makes the value equal to the text before the space occurs.

function nameField() {
  $('#nameArea').html("<br/>First Name:<input type='text' id='fnField' value=" + $('#firstName').text() + ">Last Name:<input type='text' id='lnField' value=" + $('#lastName').text() + "></input><button id='bName' onclick='updateName(fnField.value, lnField.value)'>Save Changes</button>");
  $('#firstName').html("");
  $('#lastName').html(""); 
}

So for example, if the firstName value is David E, it is currently only putting David in the field as the value. However, I have used alert and can confirm that $('#firstName').text(); does contain the full David E. How can I make sure that the text after the space doesn't get slashed out?

Share Improve this question edited Nov 2, 2018 at 8:37 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Nov 23, 2015 at 8:07 user3225440user3225440 2315 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

The issue is because you don't have any string delimiters around the value. Note the double quotes (") around the values of the attributes in this example:

$('#nameArea').html('<br/>First Name:<input type="text" id="fnField" value="' + $('#firstName').text() + '">Last Name:<input type="text" id="lnField" value="' + $('#lastName').text() + '"></input><button id="bName" onclick="updateName(fnField.value, lnField.value)">Save Changes</button>');

Alternatively you can avoid the string concatenation completely by using a Template Literal, but be aware this is unsupported in IE.

$('#nameArea').html(`<br/>First Name:<input type="text" id="fnField" value="${$('#firstName').text()}">Last Name:<input type="text" id="lnField" value="${$('#lastName').text()}"></input><button id="bName" onclick="updateName(fnField.value, lnField.value)">Save Changes</button>`);

Finally, you can tidy this HTML further by removing the onclick attribute and using unobtrusive event handlers instead. You can use data attributes to contain the necessary metadata with the element.

This is a logical error caused by the lack of quotes around the value you give.

You set the html to value=First Lastname instead of value='First Lastname'. In the first case most browsers parse the string till the whitespace and therefor only the firstname shows up.

Try

$('#nameArea').html("<br/>First Name:<input type='text' id='fnField' value='" + $('#firstName').text() + "'>Last Name:<input type='text' id='lnField' value='" + $('#lastName').text() + "'></input><button id='bName' onclick='updateName(fnField.value, lnField.value)'>Save Changes</button>");
发布评论

评论列表(0)

  1. 暂无评论