动态设置全局JS变量

时间:2019-11-27 04:24:06

标签: javascript php jquery

我想知道如何动态设置JS值。为了更好地理解我要完成的工作,这是一个php示例:

$first;
$second;
$third;

$my_array = ('first' => 'val_1','second' => 'val_2','third' => 'val_3');
foreach ($my_array as $key => $value){
  $$key = $value;
}

比方说,您有大量的输入框,它们的html格式具有唯一的ID,并且您想使用jquery或js将id用作全局变量,那让我有点困惑如何动态分配已定义的global每个语句中的变量。

我想通过js识别的html类型。

<input id='first' value='val_1'>
<input id='second' value='val_2'>
<input id='third' value='val_3'>

JS / jquery代码

var first;
var second;
var third;

$('input').each(function(){
  var item_id = $(this).attr('id');
  var item_value = $(this).attr('value');

  /* now what... */
});

在php示例中,多余的美元符号使美元说我想填充一个变量,就像存储在$ key中的变量一样。我想知道是否可以使用JS或jquery完成相同的操作。

目的是仅通过将输入添加到HTML表单来管理值的数量,而无需大量更改JS代码。

2 个答案:

答案 0 :(得分:1)

您可以使用 @Override protected void onCreate(Bundle savedInstanceState) { h = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case RECEIVE_MESSAGE: // if receive massage byte[] readBuf = (byte[]) msg.obj; String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array // and clear txtArduino.setText("Data from Arduino: " + strIncom); break; } } }; } private class ConnectedThread extends Thread { private final BluetoothSocket bluetoothSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public ConnectedThread(BluetoothSocket socket) { InputStream tmpIn = null; OutputStream tmpOut = null; bluetoothSocket = socket; // Get the input and output streams, using temp objects because // member streams are final try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } mmInStream = tmpIn; mmOutStream = tmpOut; } public void run() { byte[] buffer = new byte[1024]; // buffer store for the stream int bytes; // bytes returned from read() // Keep listening to the InputStream until an exception occurs while (true) { try { // Read from the InputStream bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer" String incomingMessage = new String(buffer, 0, bytes); Log.d(TAG, "InputStream: " + incomingMessage); h.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler } catch (IOException e) { Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() ); break; } } } } 功能

eval

例如

var first;
var second;
var third;

$('input').each(function(){
  var item_id = $(this).attr('id');
  var item_value = $(this).attr('value');
  eval(item_id+'='+item_value);
});

答案 1 :(得分:1)

这不是PHP或JavaScript的良好编程风格。只需使用数组即可:

var values = [];
$('input').each(function() {
  var item_id = $(this).attr('id');
  var item_value = $(this).attr('value');
  values[item_id] = item_value;
});
console.log('first = ' + values['first'] + ', second = ' + values['second']);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="first">First: </label>
<input id="first" type="text" value="hello" />
<label for="second">Second: </label>
<input id="second" type="text" value="world!" />
<label for="third">Third: </label>
<input id="third" type="text" />