简体   繁体   English

JavaScript附加中的单引号或双引号

[英]single or double quote in javascript append

i am beginner in Jquery, confused about using single quote or double quote to call the value of myName Textbox, i want to catch the value of text box . 我是Jquery的初学者,对使用单引号或双引号调用myName Textbox的值感到困惑,我想捕获text box的值。

this is my code, calling function save_order is not working, because failure syntax in document.getElementById( 'myName' ).value 这是我的代码,调用函数save_order不起作用,因为document.getElementById( 'myName' ).value中的失败语法

what is the right syntax ? 正确的语法是什么?

<script>
var param=3;
$('#saveList').append(
'<p>FullName : <input type="text" id="myName"></p>'+
'<p><input type="button" value="SAVE" onClick="save_order('+ param +',document.getElementById('myName').value)"></p>');
</script>

pls help me anybody thank you 请帮助我任何人谢谢

The problem is in calling document.getelementById. 问题出在调用document.getelementById。 With escaping, you can achieve your result. 通过转义,可以实现您的结果。

<script>
var param=3;
$('#saveList').append(
'<p>FullName : <input type="text" id="myName"></p>'+
'<p><input type="button" value="SAVE" onClick="save_order('+ param +',document.getElementById(\'myName\').value)"></p>');
</script>

Check your part: document.getElementById('myName') ... 检查您的部分: document.getElementById('myName') ...

And my part: document.getElementById(\\'myName\\') 我的部分是: document.getElementById(\\'myName\\')

When you use ' as start and end character of string, you have to use \\' if you want to use ' inside as string literal. 当使用'作为字符串的开始和结束字符时,如果要使用'内部作为字符串文字,则必须使用\\'

<script>
var param=3;
$('#saveList').append(
'<p>FullName : <input type="text" id="myName"></p>'+
'<p><input type="button" value="SAVE" onClick="save_order('+ param +',document.getElementById(\'myName\').value)"></p>');
</script>

You can do this or just use double quotes as that should work too. 您可以执行此操作,也可以只使用双引号,因为这样也可以。

Having code within code within code is really just begging for quoting problems, to be honest. 老实说,将代码包含在代码内实际上只是乞求引用问题。 Instead of stacking these languages on top of each other, use the jQuery library that you're already using to create the click handler for your new element. 不要将这些语言堆叠在一起,而要使用已经用于创建新元素的点击处理程序的jQuery库。

It's not entirely clear if these added elements are unique and can have id values, so let's go with a class value for the input button. 还不清楚这些添加的元素是否唯一并且可以具有id值,所以让我们为输入按钮添加一个class值。 And we'll use data- attributes for the dynamic values. 我们将使用data-属性作为动态值。 Something like this: 像这样:

var param=3;
$('#saveList').append(
    '<p>FullName : <input type="text" id="myName"></p>'+
    '<p><input type="button" class="orderButton" value="SAVE" data-name="myName" data-param="'+ param +'"></p>');

Your jQuery code can have a single click handler which responds to any "order button" click: 您的jQuery代码可以具有一个单击处理程序,该处理程序可以响应任何“订单按钮”单击:

$(document).on('click', '.orderButton', function () {
    var nameElement = $(this).data('name');
    var name = $('#' + nameElement).val();
    var param = $(this).data('param');
    save_order(param, name);
});

Keep the HTML and the JavaScript reasonable separated and the code becomes a lot cleaner and easier to maintain. 将HTML和JavaScript合理分开,代码变得更加整洁和易于维护。 I'm sure this can be cleaned up quite a bit more, but this should at least be enough to get you started. 我敢肯定,这还可以解决很多,但这至少足以让您入门。

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

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