在一行中按类设置输入类型文本值

时间:2013-04-11 06:46:12

标签: jquery html jquery-selectors

我在单选按钮中按类名进行类选择,它可以正常工作 然后我想在文本输入中按名称进行类选择,并将值设置为任何文本 我不能这样做

如何为文本框设置相同的设置值

这是我的代码

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Encuesta</title>
    <link href="css/smoothness/jquery-ui-1.9.1.custom.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <link href="css/helper.css" media="screen" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.8.2.js"></script>
    <script src="js/jquery-ui-1.9.1.custom.js"></script>

    <title>Ejemplo Radio Button</title> 
<script> 
        $(document).ready(function(){   
            var predeterminar=2;
            //This line works fine
            $("[class=radiomarcado]").filter("[value='"+predeterminar+"']").prop("checked",true);
            //$("[class=nsnr]").prop("value","NS/NR");
            //$("#p074, #p076, #p078").prop("value", "NS/NR");
            // but this does not set the value by class
            // if you can see I try other ways to do
            $('.input[type="text"][class="nsnr"]').prop("value", "NS/NR");      
            //$('input:radio[class="nsnr"]').prop("value","NS/NR");

        });
</script> 
</head>

<body> 
<form name=fcolores> 
            <table class="gridtable2">
            <caption>3.5 ¿Cuál es la cantidad promedio de empleados en el año 2011 que al interior de su empresa se dedicaron a las siguientes actividades?</caption>
                <tr><td class="subtitulos">Informática y sistemas</td>
                    <td>    <input type="text"   id="p074" name="p074" size="15" onkeypress="return RangoNumeroFormateado(this,0,0,event);" /></td>
                    <td>Si  <input type="radio" class="radiomarcado" id="p075" name="p075" value="1"/>  No<input type="radio" class="radiomarcado" id="p075" name="p075" value="2" /></td></tr>
                <tr><td class="subtitulos">Investigación y desarrollo</td>
                    <td>    <input type="text"  id="p076" name="p076" size="15" onkeypress="return RangoNumeroFormateado(this,0,0,event);" /></td>
                    <td>Si  <input type="radio"  class="radiomarcado" id="p077" name="p077" value="1"/> No<input type="radio"  class="radiomarcado" id="p077" name="p077" value="2" /></td></tr>
                <tr><td class="subtitulos">Ingeniería y diseño industrial</td>
                    <td>    <input type="text"  id="p078" name="p078" size="15" onkeypress="return RangoNumeroFormateado(this,0,0,event);" /></td>
                    <td>Si  <input type="radio"  class="radiomarcado" id="p079" name="p079" value="1"/> No<input type="radio"  class="radiomarcado" id="p079" name="p079" value="2" /></td></tr>
            </table>
</form> 
</body> 
</html>     

3 个答案:

答案 0 :(得分:6)

您正在使用带有Class Selector (“.class”)输入的 dot 。您的选择器正在查找您不想要的类输入元素。您输入了需要Element Selector (“element”)的标记,所以在这里您不应该在输入之前放置点,也不要将类nsnr分配给输入,在使用之前进行分配。

更改

$('.input[type="text"][class="nsnr"]').prop("value", "NS/NR");  

$('input[type="text"][class="nsnr"]').prop("value", "NS/NR");  

如果仅将nsnr类分配给输入,则使用简单的类选择器和val()

$('.nsnr').val("NS/NR");  

答案 1 :(得分:0)

$('.nsnr').prop("value", "NS/NR"); 

答案 2 :(得分:0)

我使用smt修复了同样的问题:

$('.nsnr').attr('value', 'NS/NR');