如何在单击按钮上更改输入文件的“接受”值

时间:2017-06-01 05:28:28

标签: javascript jquery dom

场景是我需要选择我需要上传的文件类型。选择文件类型(例如XML)后,将打开文件上载对话框,并仅将所选文件的类型过滤到XML。同样适用于单选按钮中的其他选项。我需要将单选按钮中选择的值放在文件上传的“accept”属性中。有没有办法实现这个目标?

HTML代码

<input type="radio" class="selectfileclass" name="file" id="xml" value="xml" />XML<br />
<input type="radio" class="selectfileclass" name="file" id="html" value="html" />HTML<br />
<input type="radio" class="selectfileclass" name="file" id="json" value="json" />JSON<br />

<span class="btn btn-default btn-file btn-primary">Browse<input type="file" id="ImportFile" accept=".xml" data-bind="event: { change: $root.Browse }"></span>

2 个答案:

答案 0 :(得分:8)

在单选按钮上使用<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView_stud_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:paddingLeft="8dp" android:textSize="18sp" android:textStyle="bold" android:text="111"/> <TextView android:id="@+id/textView_stud_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_below="@+id/textView_stud_name" android:paddingLeft="8dp" android:textSize="12sp" android:text="111"/> <Button android:id="@+id/btn_absence" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:background="@color/red" android:text="Absence" /> <Button android:id="@+id/btn_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/btn_absence" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:background="@color/green" android:text="Check" /> </RelativeLayout> 事件,然后设置输入值。设置&#34;接受&#34;的值像.change()

一样使用.attr()

&#13;
&#13;
.attr("accept", "." + $(this).val())
&#13;
$('.selectfileclass').change(function() {
  $('#ImportFile').attr("accept", "." + $(this).val())
})
&#13;
&#13;
&#13;

如果要选择多个参数,则必须使用复选框而不是无线电。

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" class="selectfileclass" name="file" id="xml" value="xml" />XML<br />
<input type="radio" class="selectfileclass" name="file" id="html" value="html" />HTML<br />
<input type="radio" class="selectfileclass" name="file" id="json" value="json" />JSON<br />

<span class="btn btn-default btn-file btn-primary">Browse<input type="file" id="ImportFile" accept=".xml" data-bind="event: { change: $root.Browse }"></span>
&#13;
$('.selectfileclass').click(function() {
  var s = $('.selectfileclass:checked').map(function() {
    return $(this).val()
  }).get().join(",.")
  console.log(s)
  $('#ImportFile').attr("accept", "." + s)
})
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以尝试直播活动,例如

$('input[type="radio"]').on('change',function() {
  $('#ImportFile').attr("accept", "." + $(this).val())
  console.log($(this).val());
});