document.getElementsByName('submitButton').onclick = function(event) {
event.preventDefault();
var num1 = parseFloat(document.getElementsByName('valueA')[0].value),
num2 = parseFloat(document.getElementsByName('valueB')[0].value),
arithmeticOperator = document.getElementsByName('arithmeticOperator')[0].value,
output = document.getElementsByName('result');
switch(arithmeticOperator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if(num2 != 0)
{
result = num1 / num2;
}else {
result = 'Dela inte med 0!';
}
break;
default:
result = 'Error';
}
window.alert(result);
return false;
};
我的问题是我无法获取代码来生成带答案的弹出窗口。 当我在我的HTML中使用ID它工作正常但我想学习getElementsByName如何工作。 所以,我根本不想改变我的HTML,但我需要帮助我的JS。
答案 0 :(得分:0)
getElementsByName返回一个类似于object的数组,因此您需要通过索引获取元素并添加侦听器
"http :// site.com/image.png" // from Web
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
注意:在您的页面中,我无法看到具有所述名称的按钮
由于您有表单,最好使用表单的提交事件
imageLoader.displayImage(
Uri.fromFile(new java.io.File(FILE_PATH)).toString()
,
markableImageView, mDio,
new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null & view != null) {
((ImageView) view).setImageBitmap(Bitmap.createScaledBitmap(
loadedImage,
mContext.getResources().getInteger(R.integer.width_file_view),
mContext.getResources().getInteger(R.integer.height_file_view),
false));
}
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(
String imageUri, View view, int current, int total) {
}
});
演示:Fiddle