如何将属性值调用到 javascript

How to call attribute values to javascript

本文关键字:javascript 值调用 属性      更新时间:2023-09-26
<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 中调用属性值?

我在警报框中收到错误为未定义。

我想知道调用属性以获取该值?

首先,我相信你真的不需要代码的评估,这是写在你的onclick属性中。最好在自定义属性中编写一个选择器,然后在函数中随心所欲地使用它。但是,如果您真的想按照您的要求进行操作:(尝试 Get3(

for (var i = 0; i < document.getElementsByTagName("input").length; i++) {   
 document.getElementsByTagName("input")[i].onclick = function(){
       alert(this.getAttribute("attribute"));               eval(  this.getAttribute("attribute"));     
                                                           };
                                                                       }
<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> 

或 jquery 版本

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

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

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")));
                                                               };
                                                                           }
<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> 

jquery

$( document ).ready(function() {
    $( "input" ).click(function() {
      alert($("#Name"+$(this).attr('id')).attr($(this).attr('attribute')));
});
});