如何将属性值调用到javascript

时间:2016-05-18 06:40:15

标签: javascript jquery html

<apex:form >
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="6" style="width:175px"  inputFieldId="Name" />
<input type="button" value="GET" id="0"  onclick="myfunction($('input[id$=inputFieldId]').val());"/>     
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="5" style="width:175px" id="Name1"/>  
 <input type="button" value="GET" id="0"  onclick="myfunction($('input[id$=targetField]').val());"/>     
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="4" style="width:175px" inputFieldId="Name2"/>
 <input type="button" value="GET1" id="1"  onclick="myfunction($('input[id$=valuefield]').val());"/>       
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="3" style="width:175px" inputFieldId="Name3"/>   
 <input type="button" value="GET2" id="2"  onclick="myfunction($('input[id$=valuefield]').val());"/>    
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="2" style="width:175px" inputFieldId="Name4"/>  
 <input type="button" value="GET3" id="3"  onclick="myfunction($('input[id$=SObject]').val());"/> 
</apex:form> 
<script>
function myfunction(value){
alert(value);
}
</script>  

如何在javascript中调用属性值?

我在警告框中收到未定义的错误。

我想知道call属性来获取该值吗?

1 个答案:

答案 0 :(得分:1)

首先,我相信你并不需要对代码进行评估,这是用你的onclick属性写的。最好在自定义属性中编写选择器,然后在函数中随意使用它。 但如果你真的想做你所问的问题:(试试Get3)

&#13;
&#13;
for (var i = 0; i < document.getElementsByTagName("input").length; i++) {   

 document.getElementsByTagName("input")[i].onclick = function(){
       alert(this.getAttribute("attribute"));               eval(  this.getAttribute("attribute"));     
                                                           };

                                                                       }
&#13;
<apex:form >
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="6" style="width:175px"  inputFieldId="Name" />
<input type="button" value="GET" id="0"  attribute="$('input[id$=inputFieldId]').val()"/>     
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="5" style="width:175px" id="Name1"/>  
 <input type="button" value="GET" id="0"  attribute="$('input[id$=targetField]').val()"/>     
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="4" style="width:175px" inputFieldId="Name2"/>
 <input type="button" value="GET1" id="1"  attribute="$('input[id$=valuefield]').val()"/>       
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="3" style="width:175px" inputFieldId="Name3"/>   
 <input type="button" value="GET2" id="2"  attribute="$('input[id$=valuefield]').val()"/>    
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="2" style="width:175px" inputFieldId="Name4"/>  
 <input type="button" value="GET3" id="3"  attribute="console.log(1)"/> 
</apex:form> 
&#13;
&#13;
&#13;

或jquery版本

$( document ).ready(function() {
    $( "input" ).click(function() {
  alert($(this).attr('attribute'));
  $.globalEval($(this).attr('attribute') );
});
});

但是我建议你检查你的任务并使用这样的结构:input获取元素属性的值,id = Name + input.id

&#13;
&#13;
for (var i = 0; i < document.getElementsByTagName("input").length; i++) {   

 document.getElementsByTagName("input")[i].onclick = function(){
                   alert(document.getElementById("Name"+this.getAttribute("id")).getAttribute(this.getAttribute("attribute")));
                                                               };

                                                                           }
&#13;
<apex:form >
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="6" style="width:175px"  inputFieldId="Name" id="Name0"/>
<input type="button" value="GET" id="0"  attribute="inputFieldId"/>     
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="5" style="width:175px" id="Name1"/>  
 <input type="button" value="GET" id="0"  attribute="targetField"/>     
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="4" style="width:175px" inputFieldId="Name2" id="Name2"/>
 <input type="button" value="GET1" id="1"  attribute="allowclear"/>       
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="3" style="width:175px" inputFieldId="Name3" id="Name3"/>   
 <input type="button" value="GET2" id="2"  attribute="SObject"/>    
<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="name" SObject="Quote_Line_Item__c" valuefield="name" targetField="2" style="width:175px" inputFieldId="Name4" id="Name4"/>  
 <input type="button" value="GET3" id="3"  attribute="importJquery"/> 
</apex:form> 
&#13;
&#13;
&#13;

jquery

$( document ).ready(function() {
    $( "input" ).click(function() {

      alert($("#Name"+$(this).attr('id')).attr($(this).attr('attribute')));

});
});